package hubol;

import java.util.ArrayList;

public class Animation {
	private final ArrayList<Subimage> intro;
	private final ArrayList<Subimage> loop;
	
	private float xscale = 1;
	private int image = 0;
	
	private float speed = 1;
	
	private boolean playingIntro = true;
	private int current = 0;
	private float currentProgress = 0;
	
	private boolean toLoop = true;
	
	/**create an animation with only a main loop with the contents of an array/multiple Subimages*/
	public Animation(Subimage... loop){
		this(animationFromArray(loop));
	}
	
	/**create an animation with only a main loop with the contents of an ArrayList of Subimage*/
	public Animation(ArrayList<Subimage> loop){
		this(new ArrayList<Subimage>(), loop);
	}
	
	/**create an animation with an introductory sequence and a main loop from the loop of two Animations*/
	public Animation(Animation intro, Animation loop){
		this(intro.loop, loop.loop);
	}
	
	/**create an animation with an introductory sequence and a main loop from two ArrayLists of Subimage*/
	public Animation(ArrayList<Subimage> intro, ArrayList<Subimage> loop){
		this.intro = intro;
		this.loop = loop;
		
		playingIntro = intro.size() > 0;
		currentStatus();
	}
	
	/**ensure to call this when you want to update the status of the animation*/
	public void step(){
		ArrayList<Subimage> list = getAnimation();
		if (currentProgress >= list.get(current).frameDuration){
			if (playingIntro){
				if (current + 1 >= list.size()){
					current = 0;
					playingIntro = false;
					list = getAnimation();
				}
				else
					current++;
			}
			else{
				if (current + 1 >= list.size()){
					if (toLoop)
						current = 0;
				}
				else
					current++;
			}
			currentProgress = 0;
			
			currentStatus();
		}
		currentProgress += speed;
	}
	
	private void currentStatus(){
		Subimage s = getAnimation().get(current);
		final float xs = s.xscale;
		if (xs != 9000) //this sucks
			xscale = xs;
		image = s.subimage;
	}
	
	/**used internally*/
	private ArrayList<Subimage> getAnimation(){
		if (playingIntro)
			return intro;
		return loop;
	}
	
	private static ArrayList<Subimage> animationFromArray(Subimage[] array){
		ArrayList<Subimage> list = new ArrayList<Subimage>();
		for (Subimage s : array)
			list.add(s);
		return list;
	}
	
	/**use this to retrieve the current xscale of the animation*/
	public float getXscale(){
		return xscale;
	}
	
	/**use this to retrieve the current subimage of the animation*/
	public int getSubimage(){
		return image;
	}
	
	/**use this to change the speed of the animation*/
	public void setSpeed(float spd){
		speed = spd;
	}
	
	/**set looping capabilities of primary loop*/
	public void setLooping(boolean loop){
		this.toLoop = loop;
	}
	
	/**use this to set the animation to the beginning*/
	public void reset(){
		current = 0;
		currentProgress = 0;
		playingIntro = intro.size() > 0;
		currentStatus();
	}

}
